package cn.cocho.android.mc.file;

import java.io.IOException;
import java.io.InputStream;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.util.Pair;
import android.widget.ImageView;

import cn.cocho.android.mc.log.LoggerUtils;

/**
 * 操作图片的工具类
 * 
 * @author HAIKANG SONG
 * @time 2013-5-8上午11:48:27
 */
public class ImageUtils {

	/**
	 * 获得图片的尺寸
	 * 
	 * @param imgPath
	 *            图片地址
	 * @return 第一个参数为宽度，第二个参数为高度
	 * @author HAIKANG SONG
	 * @time 2013-5-8上午11:47:16
	 */
	public static Pair<Integer, Integer> getImgSize(String imgPath) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		/*
		 * 如果我们把它设为true，那么BitmapFactory.decodeFile(String path, Optionsopt)
		 * 并不会真的返回一个Bitmap给你， 它仅仅会把它的宽，高取回来给你，这样就不会占用太多的内存
		 */
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imgPath, options);
		int height = options.outHeight;// 图片的实际高度
		int width = options.outWidth;// 图片的实际宽度
		return Pair.create(width, height);
	}

	/**
	 * 将图片添加圆角
	 * 
	 * @param imageView
	 *            存放图片的控件
	 * @param bitmap
	 *            需要转化成圆角的位图
	 * @param roundPx
	 *            圆角的度数，数值越大，圆角越大
	 * @author HAIKANG SONG
	 * @time 2013-4-19下午4:05:13
	 */
	public static void imgRoundCorner(ImageView imageView, Bitmap bitmap, int roundPx) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);// 得到画布
		int color = Color.RED;
		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
		RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// drawRoundRect的第2,3个参数一样则画的是正圆的一角，如果数值不同则是椭圆的一角
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		imageView.setImageBitmap(output);
	}

	public static int getBitmapFactorySampleSize(Context context, String imageUriPath, Pair<Integer, Integer> target) {
		Uri imageUri = Uri.parse(imageUriPath);

		return getBitmapFactorySampleSize(context, imageUri, target);
	}

	public static int getBitmapFactorySampleSize(Context context, Uri imageUri, Pair<Integer, Integer> target) {
		InputStream inputStream = null;
		try {
			ContentResolver resolver = context.getContentResolver();
			inputStream = resolver.openInputStream(imageUri);
			return getImageSampleSize(inputStream, target);
		} catch (Exception e) {
			LoggerUtils.warn("Image Utils", "Count image sample size failed!");
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				LoggerUtils.warn("Image Utils", "Close image input io failed!");
			}
		}
		return 0;
	}

	/**
	 * 获取图片的应该适合特定大小而应该缩放的比例
	 * 
	 * @param in
	 *            图片读入流
	 * @param target
	 *            目标大小
	 * @return 图片缩放的比例
	 */
	public static int getImageSampleSize(InputStream in, Pair<Integer, Integer> target) {
		Pair<Integer, Integer> imageSize = measureImageSize(in);
		int sampleSize = 0;
		if (target.second > 0) {
			sampleSize = Math.max(imageSize.first / target.second, imageSize.second / target.second);
		}
		return Math.max(sampleSize, 1);
	}

	/**
	 * 读取图片的大小，但不返回实际图片的内容
	 * 
	 * @param in
	 *            读入图片的流
	 * @return 图片的长宽(first为宽度，second为高度。)
	 */
	public static Pair<Integer, Integer> measureImageSize(InputStream in) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(in, null, opts);
		Pair<Integer, Integer> size = new Pair<Integer, Integer>(opts.outWidth, opts.outHeight);

		return size;
	}

}
